﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sharpie.Libs;
using Sharpie;

namespace SharpieAndroid.Libs
{
	public class MapButton : Button
	{
		Texture2D sideT, cornerT, centerT;
		SpriteFont FontNormal, FontNormalB, FontBigB;
		public Map LinkedMap { get; set; }

		public MapButton(Map map, Game game)
			: base(game)
		{
			LinkedMap = map;
			Initialize();
		}

		public override void Initialize()
		{
			base.Initialize();
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			sideT = Game.Content.Load<Texture2D>("Images\\mapdialog-side");
			centerT = Game.Content.Load<Texture2D>("Images\\mapdialog-center");
			cornerT = Game.Content.Load<Texture2D>("Images\\mapdialog-corner");
			FontNormal = Game.Content.Load<SpriteFont>("Fonts\\16pt");
			FontNormalB = Game.Content.Load<SpriteFont>("Fonts\\16ptB");
			FontBigB = Game.Content.Load<SpriteFont>("Fonts\\20ptB");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{
			spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

			// Button
			spriteBatch.Draw(cornerT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), null, Color.White, 0F,
				new Vector2(Rectangle.Width / 2, Rectangle.Height / 2), Scale, SpriteEffects.None, 0F);
			spriteBatch.Draw(sideT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), new Rectangle(0, 0, Rectangle.Width - cornerT.Width * 2, sideT.Height), Color.White, 0F,
				new Vector2(Rectangle.Width / 2 - cornerT.Width, Rectangle.Height / 2), Scale, SpriteEffects.None, 0F);
			spriteBatch.Draw(cornerT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), null, Color.White, 0F,
				new Vector2(-Rectangle.Width / 2 + cornerT.Width, Rectangle.Height / 2), Scale, SpriteEffects.FlipHorizontally, 0F);
			spriteBatch.Draw(sideT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), new Rectangle(0, 0, Rectangle.Height - cornerT.Width * 2, sideT.Height), Color.White, MathHelper.ToRadians(-90F),
				new Vector2(Rectangle.Height / 2 - cornerT.Width, Rectangle.Width / 2), Scale, SpriteEffects.None, 0F);
			spriteBatch.Draw(centerT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), new Rectangle(0, 0, Rectangle.Width - cornerT.Width * 2, Rectangle.Height - sideT.Height * 2), Color.White, 0F,
				new Vector2(Rectangle.Width / 2 - sideT.Height, Rectangle.Height / 2 - sideT.Height), Scale, SpriteEffects.None, 0F);
			spriteBatch.Draw(sideT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), new Rectangle(0, 0, Rectangle.Height - cornerT.Width * 2, sideT.Height), Color.White, MathHelper.ToRadians(90F),
				new Vector2(Rectangle.Height / 2 - cornerT.Width, Rectangle.Width / 2), Scale, SpriteEffects.None, 0F);
			spriteBatch.Draw(cornerT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), null, Color.White, 0F,
				new Vector2(Rectangle.Width / 2, -Rectangle.Height / 2 + cornerT.Height), Scale, SpriteEffects.FlipVertically, 0F);
			spriteBatch.Draw(sideT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), new Rectangle(0, 0, Rectangle.Width - cornerT.Width * 2, sideT.Height), Color.White, 0F,
				new Vector2(Rectangle.Width / 2 - cornerT.Width, -Rectangle.Height / 2 + sideT.Height), Scale, SpriteEffects.FlipVertically, 0F);
			spriteBatch.Draw(cornerT, new Vector2(Rectangle.Center.X, Rectangle.Center.Y), null, Color.White, 0F,
				new Vector2(-Rectangle.Width / 2 + cornerT.Width, -Rectangle.Height / 2 + cornerT.Height), Scale, SpriteEffects.FlipVertically | SpriteEffects.FlipHorizontally, 0F);

			// Text
			spriteBatch.DrawString(FontBigB, LinkedMap.Title, new Vector2((int)(Rectangle.Center.X), (int)(Rectangle.Center.Y)), Color.Black, 0F, new Vector2((int)(Rectangle.Width / 2) - 10, (int)(Rectangle.Height / 2) - 5), Scale, SpriteEffects.None, 0F);
			spriteBatch.DrawString(FontNormal, LinkedMap.Author, new Vector2((int)(Rectangle.Center.X), (int)(Rectangle.Center.Y)), Color.Black, 0F, new Vector2((int)(Rectangle.Width / 2) - 10, (int)(Rectangle.Height / 2 - FontBigB.LineSpacing) - 5), Scale, SpriteEffects.None, 0F);
			spriteBatch.DrawString(FontNormal, LinkedMap.BoardSize.Width + " x " + LinkedMap.BoardSize.Height, new Vector2((int)(Rectangle.Center.X), (int)(Rectangle.Center.Y)), Color.Black, 0F, new Vector2((int)(Rectangle.Width / 2) - 10, (int)(Rectangle.Height / 2 - FontBigB.LineSpacing - FontNormal.LineSpacing) - 5), Scale, SpriteEffects.None, 0F);
			spriteBatch.End();
		}
	}
}
